// pages/ConfirmOrder/ConfirmOrder.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var pre_order_id;
var qrOrder1;
var goods_info;
var isCart=0;
var _type;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payText:"未选择",
    order:"",
    discounts:0,
    payment:0,
    quan:true,
    _type:"",
    qrOrder1:"",
    total_fee:"",
    shipping_fee:"",
    goods_amount:"",
    voucherMoney:"",
    voucher_id:0,
    queS:true,
    adrInfo:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    console.log(options)
    if(options.type==1){
      
    }
    if (options.isCart){
      isCart=1
    }
    _type=options.type;

    qrOrder1 = JSON.parse(options.goodsInfo);
    if(_type==3){
      that.getSeckillFee()
    }else{
      that.getYunfei()
    }
    
    console.log(qrOrder1)
    this.setData({
      qrOrder1: qrOrder1,
      _type:options.type
    })
    if (_type==2){
      var goods_amount=0;
      for (var i = 0; i < qrOrder1.length; i++) {
        goods_amount += parseInt(qrOrder1[i].goods_price)
      }
      goods_amount=goods_amount.toFixed(2)
      console.log(goods_amount)
      that.setData({
        goods_amount: goods_amount,
        total_fee: goods_amount
      })
    }
    
    wx.getStorage({
      key: 'token',
      success: function(res) {
        token=res.data;
        that.getDefault()
        that.getVoucher()
      },
    })
  },
  coupon:function(e){
     var that=this;
     var total_fee = that.data.total_fee;
     var discounts= e.currentTarget.dataset.money;
    var useCondition = parseInt(e.currentTarget.dataset.usecondition)
    console.log(useCondition)
    if (total_fee - discounts <= 0 || total_fee < useCondition){
      wx.showModal({
        title: '提示',
        content: '不能使用该优惠券',
        showCancel: false,
        confirmText: '我知道了',
        confirmColor: '#DD2626',
      })
       this.setData({
         quan: true
       })
     }else{
       that.setData({
         voucher_id: e.currentTarget.dataset.id,
         discounts: e.currentTarget.dataset.money,
       })
       this.setData({
         quan: true
       })
     }
     console.log(e)
     
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  close:function(){
    this.setData({
      quan:true
    })
  },
  getPayStyle:function(){
    this.setData({
      quan: false
    })
  },
  getYunfei(){
    var that=this;

    goods_info=[]
    for (var i = 0; i < qrOrder1.length;i++){
      var goodArray={}
      goodArray.goods_id = qrOrder1[i].goods_id;
      goodArray.spec_goods_id = qrOrder1[i].spec_goods_id;
      goodArray.goods_num = qrOrder1[i].goods_num;
      if (isCart==1){
        goodArray.cart_id = qrOrder1[i].id;
      }else{
        goodArray.cart_id = 0
      }
      goods_info.push(goodArray)
    }
    console.log(goods_info)
    var goodsINfoJ = JSON.stringify(goods_info)
    // console.log(goods_info)
    wx.request({
      url: url.URl + 'goods/get_fee',
      data: { goods_info: goodsINfoJ},
      method: 'GET',
      header: {
        'Authorization': token
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            goods_amount: res.data.data.goods_amount,
            shipping_fee: res.data.data.shipping_fee,
            total_fee: res.data.data.total_fee,
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getSeckillFee(){
    var that = this;

    goods_info = []
    for (var i = 0; i < qrOrder1.length; i++) {
      var goodArray = {}
      goodArray.goods_id = qrOrder1[i].goods_id;
      goodArray.spec_goods_id = qrOrder1[i].spec_goods_id;
      goodArray.goods_num = qrOrder1[i].goods_num;
      if (isCart == 1) {
        goodArray.cart_id = qrOrder1[i].id;
      } else {
        goodArray.cart_id = 0
      }
      goods_info.push(goodArray)
    }
    console.log(goods_info)
    var goodsINfoJ = JSON.stringify(goods_info)
    // console.log(goods_info)
    wx.request({
      url: url.URl3 + 'goods/limit/get_fee',
      data: { goods_info: goodsINfoJ },
      method: 'GET',
      header: {
        'Authorization': token
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            goods_amount: res.data.data.goods_amount,
            shipping_fee: res.data.data.shipping_fee,
            total_fee: res.data.data.total_fee,
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getDefault(){
    var that=this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'my/address/get_default',
      data: {},
      method: 'GET',
      header: {
        'Authorization': token
      },
      success: function (res) {
        wx.hideLoading()
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            adrInfo:res.data.data
          })
         
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getVoucher() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/get_my_voucher',
      data: {},
      method: 'POST',
      header: {
        'Authorization': token
      },
      success: function (res) {

        wx.hideLoading()
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            couponList: res.data.data
          })
        } else if (res.data.code == 201){
          that.setData({
            queS:false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  tjOrder:function(){
    var that=this;
    var adrInfo = that.data.adrInfo;

    var addressId = that.data.adrInfo.id;

    console.log(addressId)
    if (adrInfo==""){
      wx.showToast({
        title: '请选择地址',
        icon: 'none',
      })
    }else{
      var goodsJaon = JSON.stringify(goods_info)
      var voucher_id = that.data.voucher_id;
      var _type = that.data._type;
      wx.showLoading({
        title: '加载中..',
      })
      if (_type == 1) {
        wx.request({
          url: url.URl + 'goods/submit',
          data: { goods_info: goodsJaon, address_id: addressId, voucher_id: voucher_id },
          method: 'POST',
          header: {
            'Authorization': token
          },
          success: function (res) {
            var msg = res.data.msg;
            console.log(res)
            wx.hideLoading()
            if (res.data.code == 201) {
              wx.showToast({
                title: msg,
                icon: 'none',
              })
            } else {
              wx.requestPayment({
                'appId': res.data.appId,
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  that.setData({
                    disabled: false
                  })
                  console.log(res)
                  wx.showToast({
                    title: "支付成功",
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.navigateTo({
                        url: '/pages/success/success?type=1',
                        success: function (res) { },
                        fail: function (res) { },
                        complete: function (res) { },
                      })
                    }
                  })
                },
                fail: function () {
                  that.setData({
                    disabled: false
                  })
                  wx.navigateTo({
                    url: '/pages/myOrder/myOrder?type=0',
                    // url: '/pages/success/success',
                    success: function (res) { },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              })
            }
          }
        })
      } else if (_type == 2) {
        var qrOrder1 = that.data.qrOrder1[0]
        console.log(qrOrder1.id, addressId, qrOrder1.goods_num)
        wx.request({
          url: url.URl + 'goods/exchange/tobuy',
          data: { goods_id: qrOrder1.id, address_id: addressId, goods_num: qrOrder1.goods_num },
          method: 'POST',
          header: {
            'Authorization': token
          },
          success: function (res) {
            wx.hideLoading()
            var msg = res.data.msg;
            console.log(res)
            if (res.data.code == 200) {
              wx.showToast({
                title: msg,
                icon: 'none',
                success: function () {
                  wx.navigateTo({
                    url: '/pages/myexchage/myexchage',
                    success: function (res) { },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              })

            } else if (res.data.code == 201) {
              wx.showModal({
                title: '积分不足',
                content: '购买优惠券获得积分',
                showCancel: true,
                confirmColor: '#DD2626',
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/coupon/coupon?type=1',
                      success: function (res) { },
                      fail: function (res) { },
                      complete: function (res) { },
                    })
                  } else {
                    wx.navigateBack({
                      delta: 1,
                    })
                  }
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            } else {
              wx.showToast({
                title: msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }else if(_type==3){
        wx.request({
          url: url.URl3 + 'goods/limit/tobuy',
          data: { goods_info: goodsJaon, address_id: addressId},
          method: 'POST',
          header: {
            'Authorization': token
          },
          success: function (res) {
            var msg = res.data.msg;
            console.log(res)
            wx.hideLoading()
            if (res.data.code == 201) {
              wx.showToast({
                title: msg,
                icon: 'none',
              })
            } else {
              wx.requestPayment({
                'appId': res.data.appId,
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  that.setData({
                    disabled: false
                  })
                  console.log(res)
                  wx.showToast({
                    title: "支付成功",
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.navigateTo({
                        url: '/pages/success/success?type=1',
                        success: function (res) { },
                        fail: function (res) { },
                        complete: function (res) { },
                      })
                    }
                  })
                },
                fail: function () {
                  that.setData({
                    disabled: false
                  })
                  wx.navigateTo({
                    url: '/pages/myOrder/myOrder?type=0',
                    // url: '/pages/success/success',
                    success: function (res) { },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              })
            }
          }
        })
      }
    }
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data;
        // that.getDefault()
        that.getVoucher()
      },
    })
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})